package com.reggie.dish.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.configuration.common.Result;
import com.reggie.dish.entity.Dish;
import com.reggie.dish.entity.dtos.DishDTO;
import com.reggie.dish.service.DishFlavorService;
import com.reggie.dish.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 添加菜品，用DTO（就是单独写一个类，将前端传过来的信息全部接受，在处理）方式接收菜品信息,然后将口味信息抽取出来，单独处理
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    public Result<String> insertDish(@RequestBody DishDTO dishDTO) {
        log.info("菜品信息为：{}", dishDTO);

        dishService.saveWithFlavor(dishDTO);

        return Result.success("添加成功");
    }

    /**
     * 菜品信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("page")
    public Result<Page> selectAll(int page, int pageSize, String name) {
        //为什么要返回page对象呢，因为page对象包括了所有内容
        return dishService.selectByPage(page, pageSize, name);
    }

    /**
     * 根据前端传过来的字符串切割得到ID数组进行逻辑删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> deleteByIDs(String ids) {
        log.info("要删除的ID为:{}", ids);
        List<Long> LongIDs = new ArrayList<>();
        if (ids.lastIndexOf(",") > 0) {
            String[] split = ids.split(",");
            for (int i = 0; i < split.length; i++) {
                LongIDs.add(Long.parseLong(split[i]));
            }
        } else {
            LongIDs.add(Long.parseLong(ids));
        }
        dishService.deleteByIDs(LongIDs);
        return Result.success("删除成功");
    }

    /**
     * 修改菜品信息
     *
     * @param dto
     * @return
     */
    @PutMapping
    public Result<String> updateDish(@RequestBody DishDTO dto) {
        log.info("菜品信息:{}", dto);
        dishService.updateWithFlavor(dto);
        return Result.success("修改菜品成功");
    }

    /**
     * 按照ID查询菜品及口味信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishDTO> selectByID(@PathVariable("id") Long id) {
        DishDTO dto = dishService.getByIDwithFlavor(id);
        return Result.success(dto);
    }

    /**
     * 根据分类查找菜品以及对应的口味信息
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishDTO>> selectDishByCategoryId(Dish dish) {
        log.info("分类id为{}",dish.getCategoryId());
        return dishService.selectByCategoryID(dish);
    }

    /**
     * 停售或启售菜品信息
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public Result<String> stopSellDish(@PathVariable int status, String ids) {
        log.info("要删除的ID为:{}", ids);
        List<Long> LongIDs = new ArrayList<>();
        if (ids.lastIndexOf(",") > 0) {
            String[] split = ids.split(",");
            for (int i = 0; i < split.length; i++) {
                LongIDs.add(Long.parseLong(split[i]));
            }
        } else {
            LongIDs.add(Long.parseLong(ids));
        }

        dishService.stopSellDish(status, LongIDs);
        return Result.success("操作成功");
    }

}
